﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel.Screens;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;
using B10_Ex02_EladLebovitch_034695114.ObjectModel;
using Microsoft.Xna.Framework.Input;
using Infrastructure.ServiceInterfaces;

namespace B10_Ex02_EladLebovitch_034695114.Screens
{
    public class SoundOptionsScreen : GameScreen
    {
        private Menu m_Menu;
        private InvadorBackground m_Background;
        private ISoundManeger m_SoundMgr;

        private enum ScreenMenu
        {
            FxVol,
            MusicVol,
            MusicOn,
            Done
        }

        public SoundOptionsScreen(Game i_Game)
            : base(i_Game)
        {
            m_Background = new InvadorBackground(i_Game, @"Sprites\BG_Space01_1024x768");
            this.Add(m_Background);
            m_Menu = new Menu("Sprites\\SoundMenu", i_Game, 4, 85);
            this.Add(m_Menu);
            m_Menu.MenuItemEntered += new EventHandler<MenuEventArgs>(m_Menu_MenuItemEntered);
            m_Menu.MenuItemPgDown += new EventHandler<MenuEventArgs>(m_Menu_MenuItemPgDown);
            m_Menu.MenuItemPgUp += new EventHandler<MenuEventArgs>(m_Menu_MenuItemPgUp);
        }

        public override void Initialize()
        {
            base.Initialize();
            m_SoundMgr = (ISoundManeger)this.Game.Services.
                    GetService(typeof(ISoundManeger));
            m_Menu[(int)ScreenMenu.FxVol].Value = m_SoundMgr.SoundVolume.ToString();
            m_Menu[(int)ScreenMenu.MusicVol].Value = m_SoundMgr.SoundVolume.ToString();
            m_Menu[(int)ScreenMenu.MusicOn].Value = m_SoundMgr.Mute;
        }

        private void m_Menu_MenuItemPgDown(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)ScreenMenu.FxVol:
                    m_SoundMgr.DecreaseSoundVolume();
                    m_Menu[(int)ScreenMenu.FxVol].Value = m_SoundMgr.SoundVolume.ToString();
                    break;
                case (int)ScreenMenu.MusicVol:
                    m_SoundMgr.DecreaseMusicVolume();
                    m_Menu[(int)ScreenMenu.MusicVol].Value = m_SoundMgr.MusicVolume.ToString();
                    break;
                case (int)ScreenMenu.MusicOn:
                    m_SoundMgr.ToggleMute();
                    m_Menu[(int)ScreenMenu.MusicOn].Value = m_SoundMgr.Mute;
                    break;
                default:
                    break;
            }
        }

        private void m_Menu_MenuItemPgUp(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)ScreenMenu.FxVol:
                    m_SoundMgr.IncreaseSoundVolume();
                    m_Menu[(int)ScreenMenu.FxVol].Value = m_SoundMgr.SoundVolume.ToString();
                    break;
                case (int)ScreenMenu.MusicVol:
                    m_SoundMgr.IncreaseMusicVolume();
                    m_Menu[(int)ScreenMenu.MusicVol].Value = m_SoundMgr.MusicVolume.ToString();
                    break;
                case (int)ScreenMenu.MusicOn:
                    m_SoundMgr.ToggleMute();
                    m_Menu[(int)ScreenMenu.MusicOn].Value = m_SoundMgr.Mute;
                    break;
                default:
                    break;
            }
        }

        private void m_Menu_MenuItemEntered(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)ScreenMenu.Done:
                    this.ExitScreen();
                    break;
                default:
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (this.InputManager.KeyPressed(Keys.Escape))
            {
                this.ExitScreen();
            }
            else if (this.InputManager.KeyPressed(Keys.M))
            {
                (Game.Services.GetService(typeof(ISoundManeger)) as ISoundManeger).ToggleMute();
                m_Menu[(int)ScreenMenu.MusicOn].Value = m_SoundMgr.Mute;
            }

            base.Update(gameTime);
        }
    }
}
